var outValue;
var urlParam;
var setting = {
	view : {
		dblClickExpand : false,
		showIcon : false,
		selectedMulti : false
	},
	async : {
		enable : true,
		url : urlParam,
		type : "get"
	},
	callback : {
		onClick : onClick,
		onAsyncSuccess : zTreeOnAsyncSuccess
	}
};

function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	nodes = zTree.getSelectedNodes();
	var v = "";
	var id = "";
	if (nodes.length > 0) {
		v = nodes[0].name;
		id = nodes[0].id;
	}
	var cityObj = $("#citySel");
	cityObj.attr("value", v);

	// TODO:实现比较恶心
	var channel = $("#parent_id");
	if (channel.length == 0) {
		channel = $("#channel_id");
	}
	channel.attr("value", id)
}

function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({
				left : cityOffset.left + "px",
				top : cityOffset.top + cityObj.outerHeight() + "px"
			}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "citySel"
			|| event.target.id == "menuContent" || $(event.target)
			.parents("#menuContent").length > 0)) {
		hideMenu();
	}
}

function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
	if (outValue != null) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var cityObj = $("#citySel");
		var node = zTree.getNodeByParam("id", outValue);
		cityObj.attr("value", node.name);
	}
}

$(document).ready(function() {
	$.fn.zTree.init($("#treeDemo"), setting);
});